package com.kun.portal.webframe.service;
import com.kun.portal.webframe.dao.CertificateFileDao;
import com.kun.portal.webframe.dao.CertificateMgrDao;
import com.kun.portal.webframe.po.CertificateFilePo;
import com.kun.portal.webframe.po.CertificateMgrPo;
import com.kun.portal.webframe.po.PurchasePlanPo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018-07-17.
 * 机组人员管理
 */
@Service
public class FlightCrewMgrService {
    @Autowired
    private CertificateMgrDao certificateMgrDao;
    @Autowired
    private CertificateFileDao certificateFileDao;
    public List<CertificateMgrPo> findAllCertificateMgrPo(){
        return certificateMgrDao.findAll();
    }
    public void saveCertificate(CertificateMgrPo certificateMgrPo){
        certificateMgrDao.save(certificateMgrPo);
    }
    public  List<CertificateMgrPo> checkCertificate(String userCnName , String idNumber ){

        Specification query = new Specification<CertificateMgrPo>() {
            @Override
            public javax.persistence.criteria.Predicate toPredicate(Root<CertificateMgrPo> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<javax.persistence.criteria.Predicate> pre= new ArrayList<>();
                if(StringUtils.isNotBlank(userCnName)){
                    pre.add(cb.like(root.get("fwUserPo").get("userCnName"), "%"+userCnName+"%"));
                }
                if(StringUtils.isNotBlank(idNumber)){
                    pre.add(cb.like(root.get("idNumber"), "%"+idNumber+"%"));
                }
                Predicate predicate = cb.and(pre.toArray(new Predicate[pre.size()]));
                return predicate;
            }
        };
        return certificateMgrDao.findAll(query);

    }
    public void deleteCertificate(CertificateMgrPo certificateMgrPo){
        certificateMgrDao.delete(certificateMgrPo);
    }
    public void deleteCertificateFile(CertificateFilePo certificateFilePo){
        certificateFileDao.delete(certificateFilePo);
    }


    public CertificateFilePo saveCertificateFilePo(CertificateFilePo certificateFilePo){
        return  certificateFileDao.save(certificateFilePo);
    }

}
